#include <math.h>
#include <stdlib.h>
#include "nokialcd_driver.h"
#include "nokialcd_font.h"

void sysInit();

typedef unsigned char uint8_t;
typedef   signed int   int16_t;
typedef unsigned int  uint16_t;

#define TILT (0.28 * 3.14159)
#define ORIGIN_Y (2 * LCD_HEIGHT / 3)
#define SPHEREDIAM 70
#define SPHERE_TEXW 128
#define SPHERE_TEXH 64

static uint8_t floor_texture[] = {
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11100000,
	0b00000110, 0b00100000, 0b00001100, 0b00000000, 0b00001100, 0b00000000,
	0b00001110, 0b00000000, 0b00000111, 0b11000000, 0b00000001, 0b11100000,
	0b00000000, 0b01110000, 0b00000000, 0b00110000, 0b00000000, 0b00110000,
	0b00001000, 0b01100000, 0b00001111, 0b11000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000
};

static uint8_t globe_texture[] = {
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111110, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b10000000, 0b10000000,
	0b00000000, 0b01111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b10000111, 0b10000000, 0b00000000, 0b01111111,
	0b11111001, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11101111, 0b11110000, 0b00000000, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111110, 0b00000001, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111100, 0b00011111, 0b10000011, 0b11111000,
	0b00000000, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b10110000,
	0b00000000, 0b00000011, 0b11000011, 0b11111111, 0b11111100, 0b00000011,
	0b10000010, 0b00011101, 0b10000100, 0b11111100, 0b00000011, 0b11111111,
	0b11111100, 0b00001111, 0b11111011, 0b10100000, 0b00000000, 0b00000000,
	0b00000000, 0b01110001, 0b11111100, 0b00000000, 0b00000100, 0b00000000,
	0b01011100, 0b10111000, 0b00001111, 0b11111111, 0b11111000, 0b00000111,
	0b10000000, 0b00100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b11111110, 0b00000000, 0b00000000, 0b00000001, 0b11111110, 0b11111100,
	0b01111111, 0b11111111, 0b11100001, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b11111000, 0b00000000,
	0b00000001, 0b00000011, 0b11110001, 0b11111111, 0b01111111, 0b11111111,
	0b11000001, 0b01000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000001, 0b11001111, 0b11111111, 0b01111111, 0b00000000, 0b00000011,
	0b11110000, 0b11111111, 0b11111111, 0b11111111, 0b11110011, 0b11000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b10111111,
	0b11111111, 0b11111111, 0b10000000, 0b00000000, 0b00111000, 0b00111111,
	0b11111111, 0b11111100, 0b11111110, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b11111111, 0b00111111, 0b11111111, 0b11111111,
	0b11000000, 0b00000000, 0b00010000, 0b00001111, 0b11111111, 0b11111110,
	0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00111111, 0b11111111, 0b11111111, 0b11111111, 0b11100000, 0b00000000,
	0b00000000, 0b00100111, 0b11111111, 0b11111111, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b11111111,
	0b11111111, 0b11111111, 0b11110000, 0b00000000, 0b11000000, 0b01110111,
	0b11111111, 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00111111, 0b11111111, 0b11111111, 0b11111111,
	0b11110000, 0b00000000, 0b01110000, 0b01111111, 0b11111111, 0b11111111,
	0b00000000, 0b00011000, 0b01000100, 0b00000000, 0b00000000, 0b00000000,
	0b01111111, 0b11111111, 0b11111111, 0b11111111, 0b11110000, 0b00000000,
	0b01000011, 0b11111111, 0b11111111, 0b11111000, 0b11111110, 0b11111110,
	0b01100000, 0b00000000, 0b00000000, 0b00000011, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11110000, 0b00000000, 0b00000011, 0b11111111,
	0b11111111, 0b11111000, 0b11111111, 0b11000000, 0b01100000, 0b00000000,
	0b00000000, 0b00110011, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111000, 0b00000000, 0b00000111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111000, 0b00100000, 0b00000000, 0b00000000, 0b00011111,
	0b10111111, 0b11111111, 0b11111111, 0b11111111, 0b11111100, 0b00000000,
	0b00001111, 0b11111111, 0b11111111, 0b11111100, 0b00000110, 0b11111000,
	0b00000000, 0b00000000, 0b00000000, 0b00011111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b00000000, 0b11011111, 0b11111111,
	0b11111111, 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00011111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b10000011, 0b11111111, 0b11111111, 0b11111111, 0b11100000,
	0b00000000, 0b00001000, 0b00100000, 0b00000000, 0b00000000, 0b00011111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11000111,
	0b11111111, 0b11111111, 0b11111111, 0b11100000, 0b00000000, 0b00000000,
	0b00111111, 0b00000000, 0b00000000, 0b00011111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11100011, 0b11111111, 0b11111111,
	0b11111111, 0b11100000, 0b00000000, 0b00000100, 0b00000111, 0b10000000,
	0b00000000, 0b01111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11110010, 0b01111111, 0b11111111, 0b11111111, 0b11100000,
	0b00000000, 0b00000110, 0b00001111, 0b11000011, 0b11000011, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111110,
	0b11111111, 0b11111111, 0b11111111, 0b11100000, 0b00000000, 0b00000110,
	0b00011111, 0b11000111, 0b11100001, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b10111111, 0b11111111,
	0b11111111, 0b11100000, 0b00000000, 0b00000001, 0b11111111, 0b11000111,
	0b11110001, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111100, 0b00111111, 0b11111111, 0b11100000,
	0b00000000, 0b00000000, 0b00111111, 0b11111111, 0b11111011, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11110000, 0b00001111, 0b11111111, 0b11110000, 0b00000000, 0b00000000,
	0b00111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11110000, 0b00000111,
	0b11111111, 0b11110010, 0b11000000, 0b00000000, 0b01111111, 0b11111111,
	0b11111111, 0b10111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11110000, 0b00000011, 0b11111111, 0b11111111,
	0b11100000, 0b00000000, 0b11111111, 0b11111111, 0b11100111, 0b00111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11110000, 0b00000001, 0b11111111, 0b11111111, 0b11100000, 0b00000001,
	0b11111111, 0b11111111, 0b11111110, 0b01111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11100000, 0b00000000,
	0b01111111, 0b11111111, 0b11110000, 0b00000011, 0b11111111, 0b11111111,
	0b11110011, 0b11111111, 0b10011111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11110000, 0b00000000, 0b00001111, 0b11111111,
	0b11111000, 0b00000111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11001111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11110000, 0b00000000, 0b00001111, 0b11111111, 0b11111000, 0b00000111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11110000, 0b00000000,
	0b00011111, 0b11111111, 0b11111000, 0b00000011, 0b11111111, 0b11111111,
	0b11111111, 0b11111110, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111100, 0b00000000, 0b00111111, 0b11111111,
	0b11110000, 0b00000011, 0b10111111, 0b11111111, 0b11111111, 0b11110000,
	0b11101111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111100, 0b00000000, 0b00111111, 0b11111111, 0b11110000, 0b00000111,
	0b00111111, 0b11111111, 0b11111111, 0b11110000, 0b00001111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111110, 0b00000000,
	0b00111111, 0b11111111, 0b11111000, 0b00000111, 0b01111111, 0b11111111,
	0b11111111, 0b11000000, 0b00000111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111110, 0b00000000, 0b01111111, 0b11111111,
	0b11111000, 0b00000111, 0b01111111, 0b11111111, 0b11111111, 0b10000000,
	0b00000011, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111110, 0b00000001, 0b11111111, 0b11111111, 0b11111000, 0b00001111,
	0b11111111, 0b11111111, 0b11111111, 0b10000000, 0b00000011, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111110, 0b00000001,
	0b11111111, 0b11111111, 0b11111100, 0b00011111, 0b11111111, 0b11111111,
	0b11111111, 0b10000000, 0b00000011, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111110, 0b00000011, 0b11111111, 0b11111111,
	0b11111110, 0b00111111, 0b11111111, 0b11111111, 0b11111111, 0b10000010,
	0b00000011, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111110, 0b00000111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b10000011, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111100, 0b00001111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11000011, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111100, 0b00111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111100, 0b01111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111100, 0b01111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111000, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111000, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b10111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b11111111, 0b00011111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
	0b00000000, 0b11110000, 0b00000000, 0b00000000, 0b00000111, 0b11111111,
	0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111100, 0b00111111,
	0b11111111, 0b11111100, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b11111111, 0b11111111,
	0b11111111, 0b11100000, 0b01100000, 0b00111111, 0b11111111, 0b10000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00011111, 0b11111111, 0b11111100, 0b00000000, 0b00000000,
	0b00000000, 0b00011111, 0b11111110, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111,
	0b11111110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001,
	0b10100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
	0b00000000, 0b00000000, 0b00000000, 0b00000000
};

static void drawHex8(int n, int x, int y) {
	static const char *hexchars = "0123456789ABCDEF";
	char hex[3];
	hex[0] = hexchars[(n>>4) & 0xf];
	hex[1] = hexchars[n & 0xf];
	hex[2] = 0;
	drawString(hex, 0xfff, 0x000, x, y);
}

static int16_t checkers[64];

static uint16_t spheredata[SPHEREDIAM * SPHEREDIAM] __attribute__((far));
static int sphere_top;

static void initSphere() {
	float costilt = cos(TILT);
	float sintilt = sin(TILT);
	sphere_top = ORIGIN_Y - (float)SPHEREDIAM / 2.0 * (1.0 + sin(TILT));
	int p = 0;
	for(int yi=0; yi<SPHEREDIAM; yi++) {
		float yf = 2.0 * (0.5 - (float)yi / (float)(SPHEREDIAM-1));
		for(int xi=0; xi<SPHEREDIAM; xi++) {
			float xf = 2.0 * ((float)xi / (float)(SPHEREDIAM-1) - 0.5);
			float x2y2 = xf*xf + yf*yf;
			if(x2y2 > 1.0) {
				spheredata[p++] = 0;
				continue;
			}
			float zf = sqrt(1.0 - x2y2);

			float xr = xf;
			float yr = costilt*yf + -sintilt*zf;
			float zr = sintilt*yf +  costilt*zf;
			x2y2 = xr*xr + yr*yr;
			float lat = atan2(zr, x2y2);
			float lon = atan2(yr, xr);
			int tex_i = (lon / 2.0 / 3.14159) * (float)SPHERE_TEXW;
			if(tex_i < 0) tex_i += SPHERE_TEXW;
			int tex_j = (-lat / 3.14159 + 0.5) * (float)SPHERE_TEXH;
			float shadef = (2.0*zf + yf + xf) / sqrt(4);
			shadef = shadef*shadef*shadef;
			int shade = (int)(shadef * 7.0);
			if(shade < 1) shade = 1;
			if(shade > 7) shade = 7;
			spheredata[p++] = 
				((shade & 0x7) << 13) | 
				((tex_j & 0x3f) << 7) |
				(tex_i & 0x7f);
		}
	}
}

static inline int pixavg(int a, int b) {
	int x = a & b & 0x111;
	int y = ((a & 0xeee) + (b & 0xeee)) >> 1;
	return x + y;
}

static void drawCheckers(float theta, float scale) {
	float overspin = -4.0; // must be an integer because of modulus on theta
	int sphererot = (int)((overspin * theta / 2.0 / 3.14159) * (float)SPHERE_TEXW);

	float dxdi = ( cos(theta)) / scale;
	float dydi = ( sin(theta)) / scale;
	float dxdj = (-sin(theta)) / scale;
	float dydj = ( cos(theta)) / scale;

	LCDWriteWindow(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);

	float plane_a = cos(TILT);
	float plane_b = sin(TILT);
	float plane_z0 = 0.5;

	int prev_pix = 0;
	int pix_toggle = 0;
	for(int j=0; j<LCD_HEIGHT; j++) {
		float jf = ((float)(ORIGIN_Y - j) / (float)LCD_HEIGHT);
		float depth = plane_a * plane_z0 / (plane_a - plane_b * jf);
		float plane_y = (depth - plane_z0) / plane_b;
		float plane_dx = depth / (float)LCD_HEIGHT;
		int16_t x = (32768.0 * plane_y * dxdj);
		int16_t y = (32768.0 * plane_y * dydj);
		int16_t dx = (32768.0 * plane_dx * dxdi);
		int16_t dy = (32768.0 * plane_dx * dydi);
		x -= (LCD_WIDTH/2) * dx;
		y -= (LCD_WIDTH/2) * dy;
		int shade = 15 - (int)(depth * 7.0);
		if(shade < 0) shade = 0;
		if(shade > 15) shade = 15;
		shade *= 0x111;

		for(int i=0; i<LCD_WIDTH; i++) {
			int c = -1;

			if(c == -1) {
				int idx1 = ((x >> 9) & 0xf);
				int idx2 = ((y >> 9) & 0xf);
				idx2 = (30-(idx2<<1)) + (idx1 >> 3);
				idx1 = 7 - (idx1 & 7);
				c = ((floor_texture[idx2] >> idx1) & 1) ? 0 : shade;

				idx1 = (((x >> 13)+4) & 7);
				idx2 = (((y >> 13)+4) & 7);

				c &= checkers[(idx2<<3) + idx1];
			}

			int si = i - (LCD_WIDTH - SPHEREDIAM) / 2;
			int sj = j - sphere_top;
			if(si >=0 && sj >= 0 && si < SPHEREDIAM && sj < SPHEREDIAM) {
				int dat = spheredata[(SPHEREDIAM-1-sj) * SPHEREDIAM + (SPHEREDIAM-1-si)];
				int shade = ((dat >> 13) & 0x7);
				if(shade) {
					shade += 8;
					shade *= 0x111;
					int tex_j = ((dat >> 7) & 0x3f);
					int tex_i = (dat & 0x7f);
					tex_i += sphererot;
					tex_i += SPHERE_TEXW/2;
					tex_i &= (SPHERE_TEXW-1);
					tex_j = SPHERE_TEXH-1-tex_j;

					int idx2 = (tex_j << 4) + (tex_i >> 3);
					int idx1 = 7 - (tex_i & 7);
					c = ((globe_texture[idx2] >> idx1) & 1) ? c : 0xff0;

					dat = spheredata[sj * SPHEREDIAM + si];
					shade = ((dat >> 13) & 0x7);

					shade *= 0x111;
					tex_j = ((dat >> 7) & 0x3f);
					tex_i = (dat & 0x7f);
					tex_i += sphererot;
					tex_i &= (SPHERE_TEXW-1);

					idx2 = (tex_j << 4) + (tex_i >> 3);
					idx1 = 7 - (tex_i & 7);
					c = ((globe_texture[idx2] >> idx1) & 1) ? 
						pixavg((0x00f & (shade<<1)), c) : (0x0f0 | (0xf0f & shade));
				}
			}

			if(pix_toggle & 1) {
				LCDWritePair(prev_pix, c);
			} else {
				prev_pix = c;
			}
			pix_toggle++;
			x += dx;
			y += dy;
		}
	}
}

static int randColor() {
	int c = 0;
	while(!c) {
		c =
			((rand() & 1) ? 0xf00 : 0) +
			((rand() & 1) ? 0x0f0 : 0) +
			((rand() & 1) ? 0x00f : 0);
	}
	return c;
}

int main() {
	sysInit();
	LCDInitController();

	for(int i=0; i<64; i++) checkers[i] = randColor();
	initSphere();
	float theta = 0;
	int iter = 0;
	for(;;) {
		drawCheckers(theta, 0.5);
		drawHex8(iter, 0, LCD_HEIGHT-8);
		theta += 0.05;
		if(theta > 2*3.14159) theta -= 2*3.14159; // prevent overflow
		checkers[iter & 63] = randColor();
		checkers[(iter+1) & 63] = 0xfff;
		iter++;
	}
}
